#include <string>
#include <vector>
#include <iostream>
using namespace std;
#include "utils/string.h"
#include "utils/to_ostream.h"
#include "utils/cmd_arg.h"
using namespace tdl;

struct uinfo
{
	unsigned char	stx;
	uint32_t		uin;
	uint32_t		cip;
	char			lc[16];
	unsigned char	etx;
public:
	void encode(char* p, size_t& l)
	{
		l = 0;
		*(unsigned char*)p = stx; p++; l++;
		*(uint32_t*)p = uin; p+=4; l+=4;
		*(uint32_t*)p = cip; p+= 4; l+=4;
		memcpy(p, lc, 16); p+=16; l+=16;
		*(unsigned char*)p = etx; l++;
	}
};
#include <arpa/inet.h>
//////////////////////
int main(int argc, char* argv[])
{
	vector<cmd_arg> vca;
	vca.push_back(cmd_arg("help", false, 'h'));	
	vca.push_back(cmd_arg("daemon", false, 'd'));	
	vca.push_back(cmd_arg("config", true, 'c'));
	map<char, std::string> mca = parse_cmd_args(vca, argc, argv);
	cout << mca << endl;

	cout << u2s(-8379453, 10) << endl;
	cout << u2s(8379453, 16) << endl;
	cout << u2s(8379453, 16, true) << endl;

	cout << i2s(-8379453, 10) << endl;

	cout << ul2s(8459343957343, 10) << endl;
	cout << ul2s(8459343957343, 16) << endl;
	cout << ul2s(8459343957343, 16, true) << endl << endl;

	tdl::small_buffer<100> sb;

	uinfo ui;
	ui.stx = 0x02;
	ui.uin = htonl(5081524);
	ui.cip = htonl(3232235778);
	memcpy(ui.lc, "1234567890123456", 16);
	ui.etx = 0x03;
	ui.encode(sb.ptr, sb.len);
	cout << sb << endl << endl;
	cout << bin2s(sb.ptr, sb.len, true, false) << endl << endl;
	cout << bin2s(sb.ptr, sb.len, false, true) << endl << endl;
	cout << bin2s(sb.ptr, sb.len, false, false) << endl << endl;

	string s = "udp@192.168.1.100:9527";
	vector<string> vs;
	string tmps(s.data());
	split_string(tmps, "@:", vs);
	cout << vs << endl;
	cout << s << endl;

	msec_t ms = 0;
	time_t t = time(0);
	cout << t2s_datetime(t, &ms) << endl;
	cout << t2s_date(t) << " " << t2s_time(t) << endl;

	struct timeval tv;
	::gettimeofday(&tv, NULL);
	string st = t2s_datetime(tv);
	cout << st << endl;

	struct tm sptm = s2t_datetime(st);
	cout << sptm.tm_year << "-" << sptm.tm_mon << "-" << sptm.tm_mday << " "
		 << sptm.tm_hour << ":" << sptm.tm_min << ":" << sptm.tm_sec << endl;
	cout << t2s_datetime(sptm) << endl;
	return 0;
}

